package client.comms;

import org.apache.commons.fileupload.MultipartStream;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.http.entity.mime.MultipartEntity;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;

/**
 * Created by IntelliJ IDEA.
 * User: sam
 * Date: 28/04/12
 * Time: 11:20
 * To change this template use File | Settings | File Templates.
 */

public class ProgressMultipartEntity extends MultipartEntity {

    public final ProgressListener listener;

    public ProgressMultipartEntity(HttpMultipartMode BROWSER_COMPATIBLE, ProgressListener listener) {
        super(BROWSER_COMPATIBLE);
        this.listener = listener;
    }

    @Override
    public void writeTo(final OutputStream outstream) throws IOException {
        super.writeTo(new CountingOutputStream(outstream, getContentLength(), listener));
    }

    public static class CountingOutputStream extends FilterOutputStream {

        private final ProgressListener listener;
        private long transferred;
        private double contentLen;

        public CountingOutputStream(final OutputStream out, long contentLen, ProgressListener listener) {
            super(out);
            transferred = 0;
            this.contentLen = contentLen/1024;
            this.listener = listener;
        }

        public void write(byte[] b, int off, int len) throws IOException {

            out.write(b, off, len);
            transferred += len;
            double written = transferred/1024;
            double tmp = (written/contentLen);
            long progress = (long) (tmp * 100);

            this.listener.update(progress, progress, len);
        }
    }
}


/**
 * import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;

public class CountingMultiPartEntity extends MultipartEntity {

    private UploadProgressListener listener_;
    private CountingOutputStream outputStream_;
    private OutputStream lastOutputStream_;

    // the parameter is the same as the ProgressListener class in tuler's answer
    public CountingMultiPartEntity(UploadProgressListener listener) {
        super(HttpMultipartMode.BROWSER_COMPATIBLE);
        listener_ = listener;
    }

    @Override
    public void writeTo(OutputStream out) throws IOException {
        // If we have yet to create the CountingOutputStream, or the
        // OutputStream being passed in is different from the OutputStream used
        // to create the current CountingOutputStream
        if ((lastOutputStream_ == null) || (lastOutputStream_ != out)) {
            lastOutputStream_ = out;
            outputStream_ = new CountingOutputStream(out);
        }

        super.writeTo(outputStream_);
    }

    private class CountingOutputStream extends FilterOutputStream {

        private long transferred = 0;
            private OutputStream wrappedOutputStream_;

        public CountingOutputStream(final OutputStream out) {
            super(out);
                    wrappedOutputStream_ = out;
        }

        public void write(byte[] b, int off, int len) throws IOException {
                    wrappedOutputStream_.write(b,off,len);
                    ++transferred;
            listener_.transferred(transferred);
        }

        public void write(int b) throws IOException {
            super.write(b);
        }
    }
}

 */
